<?php
header("Content-Type: text/html; charset=iso-8859-1");
class document{
	
	var $fields;
	
	function document($value = '',$field='id'){
		$fields = describe('documents');
		$q = "select * from documents where $field = '$value'";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
		}
		else{
			foreach($fields as $field => $type){
				$this->$field = "";
			}
		}
	}
	
	function insert(){
		$fields = describe('documents');
		$q = "insert into documents (";
		$q1 = ") values(";
		foreach($fields as $field => $type){
			$q .= $field.", ";
			$q1 .= "\"".(addslashes($this->$field))."\", ";
		}
		$q = substr($q,0,-2);
		$q1 = substr($q1,0,-2);
		$q .= $q1.")";
		if(ExecRequete($q)) return mysql_insert_id();
		else  return false;
	}
	
	function update(){
		$fields = describe('documents');
		if($this->id){
			$q = "update documents set ";
			foreach($fields as $field => $type)	{
				$q .= "$field = \"".(addslashes($this->$field))."\", ";
			}
			$q = substr($q,0,-2);
			$q .= " where id = '$this->id'";
			if(ExecRequete($q)) return true;
			else return false;
		}
		else $this->insert();
	}
	
	function delete(){
		$q = "delete from documents where id = '$this->id'";
		if(ExecRequete($q)){
			if(file_exists("documents/$this->fichier")) unlink("documents/$this->fichier");
			return true;
		}
		else return false;
	}
	
	function print_link(){
		$doctype = (ereg(".*\.([a-zA-Z]*)",$this->fichier,$regs)) ? strtolower($regs[1]) : "txt";
		return "<a href = 'documents/$this->fichier'><img src = 'images/icon_$doctype.png' alt = '$doctype' width = '14' height = '14' />&nbsp;$this->fichier</a>\n";
	}
	
	function icon(){
		$doctype = (ereg(".*\.([a-zA-Z]*)",$this->fichier,$regs)) ? strtolower($regs[1]) : "txt";
		return "<img src = 'images/icon_$doctype.png' alt = '$doctype' width = '14' height = '14' />\n";
		
	}

	function date($format = 'd.m.Y'){
		$filename = "documents/".$this->fichier;
		if (file_exists($filename)) {
		    return date ($format, filemtime($filename));
		}
		else return "inconnu";
	}
}
?>